#include <string>
#include <random>
#include <ctime>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string.h>
 #include <time.h>

using namespace std; //注意，命名空间 std

std::mt19937 rnd(time(0));

struct restaurantInfo{
  std::string restaurantName;
  std::string congestionTime;
  std::string weights;
};

restaurantInfo altResta[100];
vector<string> cachedResta;

int readRestaurantInfo(const char * filename, restaurantInfo restaArr[])
{
  ifstream infile(filename);
  string line;
  getline(infile, line);
  int restaCnt = 0;
  while (getline(infile, line))
  {
    istringstream sin(line);
    vector<string> fields;
    string field;
    while (getline(sin, field, ',')) 
    {
      fields.push_back(field);
    }
    restaArr[restaCnt].restaurantName = fields[0];
    restaArr[restaCnt].congestionTime = fields[1];
    restaArr[restaCnt].weights = fields[2];
    // cout << restaArr[restaCnt].restaurantName << restaArr[restaCnt].congestionTime << restaArr[restaCnt].weights << endl;
    restaCnt++;
  }

  return restaCnt;
}

void readCachedInfo(const char * filename, vector<string> &cachedPoint) {
  ifstream infile(filename);
  string line;
  while(getline(infile, line)){
    cachedPoint.push_back(line);
  }
}

void writeCachedInfo(string selectedResta){
  fstream writedfile;
  writedfile.open("src/data/cached.hist", ios::out|ios::app);
  writedfile<< selectedResta <<endl;
  writedfile.close();
}

void cleanCachedInfo(){
  fstream writedfile;
  writedfile.open("src/data/cached.hist", ios::out);
  writedfile<< "" <<endl;
  writedfile.close();
}

bool tellmeIsitMonday(){
  time_t timer;
  struct tm *tblock;
  timer = time(NULL);
  tblock = localtime(&timer);
  cout <<"\n Local time is:" << asctime(tblock) << endl;
  string timeStr = asctime(tblock);
  bool isMonday = false;
  int index = timeStr.find("Mon");
  if(index >= 0){
    isMonday = true;
  } else {
    isMonday = false;
  }

  return isMonday;
}


int selector_gen(int restaNum){
  readCachedInfo("src/data/cached.hist", cachedResta);
  long randomNum = 0;
  int sel = 0;
  bool hasCached = false;
  vector<std::string>::iterator iter;
  do{
    randomNum = rnd();
    sel = randomNum % restaNum;
    for(iter = cachedResta.begin(); iter != cachedResta.end(); ++iter){
      if(*iter == altResta[sel].restaurantName){
        hasCached = true;
        cout << sel << " " << *iter << " has been cached " << endl;
        break;
      } else if(iter == cachedResta.end()-1){
        hasCached = false;
      }
    }
  }while(hasCached);

  return sel;
}


int main()
{
  
  bool isMonday = tellmeIsitMonday();

  if(isMonday){
    cleanCachedInfo();
  }

  int restaNum = readRestaurantInfo("src/data/restaurant.info", altResta);
  int sel = selector_gen(restaNum);
  printf("\nGood afternoon! restaurant = %s\n\n", altResta[sel].restaurantName.c_str());

  cout << "<y/n>";
  char ch;
  cin >> ch;
  if (ch == 'y' || ch == 'Y'){
    cout << "OK! \n";
    writeCachedInfo(altResta[sel].restaurantName);
  }
  else if (ch == 'n' || ch == 'N')
    cout << "sorry! \n";
  else
    cout << "exit \n";

  return 0;
}

